<?php
/***********************************************************************
 * @package    	IvanEngine
 * @subpackage 	Core
 * @author     	Ivan <ivanzx@msn.com>
 * @created    	2010-05-19
 ***********************************************************************/
if(defined("IVANENGINE_CORE_VALIDATOR_NUMBER"))
	die("Redefinition macro in :".__FILE__."(".__LINE__.")");
define("IVANENGINE_CORE_VALIDATOR_NUMBER", true);

require_once (dirname(__FILE__).'/ValidatorBase.php');

class CValidatorNumber extends CValidatorBase
{
	const NM_Max		= 'NM_Max';
	const NM_Min		= 'NM_Min';
	const NO_Max		= 'NO_Max';
	const NO_Min		= 'NO_Min';
	
	protected function Initialize($options = array(), $messages = array())
  	{
  		parent::Initialize($options, $messages);

  		$this->AddMessage(self::NM_Max, '"%value%" must be less than %'.self::NO_Max.'%.');
	    $this->AddMessage(self::NM_Min, '"%value%" must be greater than %'.self::NO_Min.'%.');
	
	    $this->AddOption(self::NO_Min);
	    $this->AddOption(self::NO_Max);
	
	    $this->SetMessage(self::NM_Invalid, '"%value%" is not a number.');
  	}
  	
	protected function DoVerify($value) 
	{
		if (!is_numeric($value))
	    {
	      	throw new CExceptionValidator($this, self::NM_Invalid, array('value' => $value));
	    }
	
	    $clean = floatval($value);
	
	    if ($this->HasOption(self::NO_Max) && $clean > $this->GetOption(self::NO_Max))
	    {
	      throw new CExceptionValidator($this, self::NM_Max, array('value' => $value, self::NO_Max => $this->GetOption(self::NO_Max)));
	    }
	
	    if ($this->HasOption(self::NO_Min) && $clean < $this->GetOption(self::NO_Min))
	    {
	      throw new CExceptionValidator($this, self::NM_Min, array('value' => $value, self::NO_Min => $this->GetOption(self::NO_Min)));
	    }
	
	    return $clean;
	}
}

?>